﻿using MVC_No03.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVC_No03.Controllers
{
    public class MyHomeController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            using (Model1 model = new Model1())
            {
                List<Task> a = model.Task.Where(u => true).ToList();

                return View(a);
            }
        }

        [HttpGet]
        public ActionResult GetArrInfo(string uid)
        {
            if (!string.IsNullOrEmpty(uid)) {
                if (int.TryParse(uid, out int i)) {
                    using (Model1 model = new Model1())
                    {
                        var id = Convert.ToInt32(uid);
                        List<Task> a = model.Task.Where(u => u.TaskID == id).ToList();

                        return View(a);
                    }
                }
            }
            return View(new List<Task>());
        }

        [HttpPost]
        public ActionResult DeletePage(string uid) {
            if (!string.IsNullOrEmpty(uid))
            {
                if (int.TryParse(uid, out int i))
                {
                    using (Model1 model = new Model1())
                    {
                        var id = Convert.ToInt32(uid);
                        model.Task.Remove(model.Task.FirstOrDefault(u => u.TaskID == id));
                        var p = model.SaveChanges();


                        return Json(new { State = p});
                    }
                }
            }
            return Json(new { State = 0 });
        }
    }
}